﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

namespace RemoteClient.UI
{
    public partial class DJobItem : ListViewItem
    {
        
        private RemoteObject.DownloadJob dJob;
        private ListViewSubItem nameItem;
        private ListViewSubItem sizeItem;
        private ListViewSubItem urlItem;
        private ListViewSubItem processItem;
        private ListViewSubItem rateItem;
        private ListViewSubItem stateItem;

        private Timer updateTimer;

        public RemoteObject.DownloadJob DJob
        {
            get
            {
                return dJob;
            }
            set
            {
                dJob = value;
                InitSubItems();
            }
        }

        public Timer UpdateTimer
        {
            get 
            {
                return updateTimer;
            }
            set {
                updateTimer = value;
            }
        }        

        public DJobItem(RemoteObject.DownloadJob dJob)
        {
            this.dJob = dJob;
            InitSubItems();            
            //InitTimer();
            //UpdateTimer.Start();
        }        

        private void InitSubItems()
        {            
            nameItem = new ListViewSubItem(this, dJob.GetJobName());
            sizeItem = new ListViewSubItem(this, dJob.GetFileSizeInMB().ToString("f2")+" M");
            urlItem = new ListViewSubItem(this, dJob.GetServerURL().ToString());
            processItem = new ListViewSubItem(this, dJob.GetProgress().ToString("f2")+"%");
            rateItem = new ListViewSubItem(this, dJob.GetTransferSpeed().ToString("f2") + " KB/s");
            stateItem = new ListViewSubItem(this, dJob.GetStatus().ToString());

            SubItems.Clear();
            SubItems.Insert(0,nameItem);
            SubItems.Insert(1,sizeItem);
            SubItems.Insert(2,urlItem);
            SubItems.Insert(3,processItem);
            SubItems.Insert(4,rateItem);
            SubItems.Insert(5,stateItem);
        }

        private void InitTimer()
        {
            updateTimer = new Timer();
            updateTimer.Tick += new EventHandler(UpdateTimer_Tick);
            updateTimer.Interval = 2000;
        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            Console.WriteLine(dJob.GetJobName() + "-----Update Job Info--------------");
            RefreshJobInfo();
        }

        public void RefreshJobInfo()
        {
            processItem.Text = dJob.GetProgress().ToString("f2") + "%";
            rateItem.Text = dJob.GetTransferSpeed().ToString("f2") + " KB/s";
            stateItem.Text = dJob.GetStatus().ToString();
        }


        public void Dispose()
        {
            if (updateTimer != null)
            {
                updateTimer.Stop();
                updateTimer.Dispose();
            }
        }
    }
}
